/************************************************
 * 												*
 * Project	:	Family Entertainment 			*
 * Source	:	DAO Class Service Type			*
 * Owner	:	Vially Flores					*
 * 												*
 ************************************************/

package upcweb.dao;

import upcweb.modelo.ClsTipoServicio;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import upcweb.util.ClsReturn;

public class TipoServicioDAO
{
	private SQLProcDAO sqlDAO = new SQLProcDAO();

	public ClsReturn insertTypeService(ClsTipoServicio clTService)
	{
		return sqlDAO.execQuery("insert into tiposervicio (co_tipo_servicio, no_tipo_servicio, fl_activo) values (?,?,?)", 
				new Object[]{clTService.getIdTypeService(), clTService.getName(), clTService.getState()});
	}

	public ClsReturn updateTypeService(ClsTipoServicio clTService)
	{
		return sqlDAO.execQuery("update tiposervicio set no_tipo_servicio = ?, fl_activo = ? where co_tipo_servicio = ?",
				new Object[]{clTService.getName(), clTService.getState(), clTService.getIdTypeService()});
	}
	
	public ClsReturn logicaldeleteTypeService(ClsTipoServicio clTService)
	{
		return sqlDAO.execQuery("update tiposervicio set fl_activo = ? where co_tipo_servicio = ?", new Object[]{clTService.getState(), clTService.getIdTypeService()});
	}

	public ClsReturn physicaldeleteTypeService(ClsTipoServicio clTService)
	{
		return sqlDAO.execQuery("delete from tiposervicio where co_tipo_servicio = ?", new Object[]{clTService.getIdTypeService()});
	}
	
	public int newIndexTypeService(ClsTipoServicio clTService)
	{
		try {			
			CallableStatement cs = ConexionDAO.newConexion().prepareCall("{call newidservicetype(?, ?, ?)}");
			cs.setString(1, clTService.getName());
			cs.registerOutParameter(2, Types.INTEGER);
			cs.registerOutParameter(3, Types.INTEGER);
			cs.execute();
			//System.out.println("Major Value in DDBB: " + cs.getInt(2)); // returns the highest rate recorded in the database.
			System.out.println("New ID: " + cs.getInt(3)); // returns a new valid index. if value is 0: Can not insert new row.
			return cs.getInt(3);
		}
		catch (Exception e) {
			// TODO: handle exception
		      System.err.println("<VIFLO>: Error " + e.getMessage());
		      e.printStackTrace();
		      return 0;
		}
	}

	public ArrayList<ClsTipoServicio> ListTypeService_All()
	{
		ArrayList<ClsTipoServicio> arrayTypeService = new ArrayList<ClsTipoServicio>();
		ClsTipoServicio clTService;

		try {
			ResultSet rs = sqlDAO.getData("select co_tipo_servicio, no_tipo_servicio, fl_activo from tiposervicio", new Object[]{});
			while(rs.next()){
				clTService = new ClsTipoServicio();
				clTService.setIdTypeService(rs.getInt(1));
				clTService.setName(rs.getString(2));
				clTService.setState(rs.getString(3));
				arrayTypeService.add(clTService);	
			}	
		} catch (Exception e) {
			// TODO: handle exception
		}		
		return arrayTypeService;
	}

	public ArrayList<ClsTipoServicio> ListTypeService_byName(String inputCharacters)
	{
		ArrayList<ClsTipoServicio> arrayTypeService = new ArrayList<ClsTipoServicio>();
		ClsTipoServicio clTService;

		try {
			ResultSet rs = sqlDAO.getData("SELECT co_tipo_servicio, no_tipo_servicio, fl_activo from tiposervicio where no_tipo_servicio like ?", new Object[]{inputCharacters});
			while(rs.next()){
				clTService = new ClsTipoServicio();
				clTService.setIdTypeService(rs.getInt(1));
				clTService.setName(rs.getString(2));
				clTService.setState(rs.getString(3));
				arrayTypeService.add(clTService);	
			}	
		} catch (Exception e) {
			// TODO: handle exception
		}		
		return arrayTypeService;
	}
	
	public ArrayList<ClsTipoServicio> ListTypeService_byState(Integer inputCharacters)
	{
		ArrayList<ClsTipoServicio> arrayTypeService = new ArrayList<ClsTipoServicio>();
		ClsTipoServicio clTService;

		try {
			ResultSet rs = sqlDAO.getData("SELECT co_tipo_servicio, no_tipo_servicio, fl_activo from tiposervicio where fl_activo = ?", new Object[]{inputCharacters});
			while(rs.next()){
				clTService = new ClsTipoServicio();
				clTService.setIdTypeService(rs.getInt(1));
				clTService.setName(rs.getString(2));
				clTService.setState(rs.getString(3));
				arrayTypeService.add(clTService);	
			}	
		} catch (Exception e) {
			// TODO: handle exception
		}		
		return arrayTypeService;
	}
	
	/**** 02/12/2012*****/
	public ArrayList<ClsTipoServicio> ListTypeService_Filtro(String parametro[])
	{
		ArrayList<ClsTipoServicio> arrayTypeService = new ArrayList<ClsTipoServicio>();
		ClsTipoServicio clTService;

		try {
			String query="select co_tipo_servicio, no_tipo_servicio, fl_activo from tiposervicio where 1=1 ";
			String estado="";
			if (parametro.length > 0) {
				/* Filtros ---> [0]=Nombre , [1] Estado * */
				query += " and no_tipo_servicio like ? ";
				if (parametro[1].equals("2")) {
					estado = "0,1";
				} else {
					estado = parametro[1];
				}				
				query += " and fl_Activo in ( " + estado + " )";
				parametro[0]="%" + parametro[0]+"%";
			} 					
			ResultSet rs = sqlDAO.getData(query, new Object[]{parametro[0]});
			while(rs.next()){
				clTService = new ClsTipoServicio();
				clTService.setIdTypeService(rs.getInt(1));
				clTService.setName(rs.getString(2));
				clTService.setState(rs.getString(3));
				arrayTypeService.add(clTService);	
			}	
		} catch (Exception e) {
			// TODO: handle exception
		}		
		return arrayTypeService;
	}
	
	
}
